package com.xuecheng.content.controller;

import com.xuecheng.commons.model.vo.CoursePubVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CoursePub;
import freemarker.template.TemplateException;
import io.minio.errors.*;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

/**
 * <p>
 * 课程发布前端控制器
 * </p>
 *
 * @author HuangYuwu
 */
@Slf4j
@RestController
public class CoursePubController {

    @Autowired
    private CoursePubService  coursePubService;

    /**
     * 课程发布预览
     * @param courseBaseId
     * @return
     */
    @GetMapping("/coursePub/preview/{courseBaseId}")
    public ModelAndView preview(@PathVariable Long courseBaseId){
        Map model=coursePubService.preview(courseBaseId);
        ModelAndView modelAndView=new ModelAndView("learing_article",model);
        return modelAndView;
    }

    /**
     * 课程发布
     * @param courseBaseId
     * @return
     */
    @PostMapping("/coursePub/publish/{courseBaseId}")
    public ResponseResult publish(@PathVariable Long courseBaseId) throws TemplateException, IOException {
        return coursePubService.publish(courseBaseId);
    }

    /**
     * 根据id查询课程发布
     * @param id
     * @return
     */
    @GetMapping("/coursePub/{id}")
    public ResponseResult findById(@PathVariable("id") Long id){
        CoursePub coursePub = coursePubService.getById(id);
        CoursePubVo vo = BeanHelper.copyProperties(coursePub, CoursePubVo.class);
        return ResponseResult.okResult(vo);
    }

    /**
     * 已发布课程下架
     * @param courseId
     * @return
     */
    @GetMapping("/course/offline/{courseId}")
    public ResponseResult offlineById(@PathVariable("courseId") Long courseId) throws ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return coursePubService.offlineById(courseId);
    }
}
